package com.xiaolong.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xiaolong.util.ValidateCodeUtil;

/**
 * 验证码生成器
 * @author HH
 *
 */
@WebServlet(urlPatterns="/validatecodeServlet")
public class ValidatecodeServlet extends HttpServlet{
	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override  
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
	        System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");  
	        doPost(req, resp);  
	    }  
	  
	    @Override  
	    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
	        System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");  
	        int width = 60;
	    	int height = 32;
	    	int length = 4;
	    	// 创建BufferedImage对象  
            Font font=new Font("宋体",Font.BOLD,20);  
	    	//create the image
	    	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	    	Graphics2D g = image.createGraphics();
	    	// set the background color
	    	g.setColor(new Color(0xDCDCDC));
	    	g.fillRect(0, 0, width, height);
	    	// draw the border
	    	g.setColor(Color.gray);
	    	g.drawRect(0, 0, width - 1, height - 1);
	    	// create a random instance to generate the codes
	    	Random rdm = new Random();
	    	String baseCharacters = Integer.toHexString(rdm.nextInt());
	    	System.out.print(baseCharacters);
	    	int len = baseCharacters.length(); 
	    	// make some confusion
	    	for (int i = 0; i < 50; i++) {
	    		int x = rdm.nextInt(width);
	    		int y = rdm.nextInt(height);
	    		g.drawOval(x, y, 0, 0);
	    	}
	    	/*ValidateCodeUtil.drawLine(g, 20, width, height);
	    	for (int i = 0; i < length; i++) {
	            // 随机生成验证码中单个字符
	            String randStr = String.valueOf(baseCharacters.charAt(rdm
	                    .nextInt(len)));
	            // 单个字符绘制宽度
	            int width1 = width / length;
	            // 当前字符绘制原点
	            int x = width1 * i;
	            int y = height / 2 + rdm.nextInt(height / 3);
	             将该字符画到图像中 
	            ValidateCodeUtil.drawString(g, x, y, "和");
	        }*/
	    	// generate a random code
	    	String capstr = baseCharacters.substring(0, 4);
	    	HttpSession session = req.getSession(true);
	    	//将生成的验证码存入session
	    	session.setAttribute("validateCode", capstr);
	    	g.setColor(new Color(0, 100, 0));
	    	g.setFont(new Font("Candara", Font.BOLD, 24));
	    	g.drawString(capstr, 8, 20);
	    	/*g.setFont(font);
	    	g.rotate(30 * Math.PI / 180);
	    	g.drawString("和", 8, 10);
	    	g.setFont(new Font("楷体",Font.BOLD,20));
	    	g.translate(30,20);
	    	g.rotate(-60 * Math.PI / 180);
	    	g.drawString("好", 10, 5);*/
	    	g.dispose();
	    	//输出图片
	    	resp.setContentType("image/jpeg");
	    	OutputStream strm = resp.getOutputStream();
	    	ImageIO.write(image, "jpeg", strm);
	    	strm.close();   
	       
	    }  
}
